package com.sicheng.lc.杂题.滑动窗口专题;

/**
 * @author zsc
 * @version 1.0
 * @date 2022/8/3 10:18
 */
public class 区间子数组个数 {
    //https://leetcode.cn/problems/number-of-subarrays-with-bounded-maximum/
    public int numSubarrayBoundedMax(int[] nums, int left, int right) {
        return cnt(nums, right) - cnt(nums, left - 1);
    }

    int cnt(int[] nums, int k) {
        int res = 0;
        for (int r = 0, l = 0; r < nums.length; r++) {
            if (nums[r] > k) {
                l = r + 1;
            }
            res += r - l + 1;
        }
        return res;
    }
}
